import { createSlice } from '@reduxjs/toolkit';

const mySlice = createSlice({
    //命名空间，在调用action的时候会默认的设置为action的前缀,保证唯一不重名
    name: 'mySlice',
    //初始化状态
    initialState: {
        counter: 100,
        user: {
            name: "hm",
            job: "全栈",
        },
    },
    reducers: {
        increment(state, actions) {
            const { type, payload } = actions
            state.counter = state.counter + payload;
        },

        updateUser(state, actions) {
            /**
             * type:相当于唯一标识
             * payload：传递（dispatch）过来的参数
             */
            const { type, payload } = actions
            state.user = { ...state.user, ...payload, };

        },
    },
});

export const { increment, updateUser } = mySlice.actions; // 导出 action creator
export default mySlice.reducer; // 导出 reducer